"""author: wenyao
   data: 2021/3/25
   project: sdyxzteacher
"""

#列表
#有序项目集合
#可以存放任何类型对象
#可变数据类型

#####################列表定义############################
# >>> lst = []
# >>> type(lst)
# <class 'list'>
# >>> lst2 = list()
# >>> type(lst2)
# <class 'list'>
# >>> lst
# []
# >>> lst2
# []
# >>> lst3 = list("abc")
# >>> lst3
# ['a', 'b', 'c']
# >>> list(123)
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: 'int' object is not iterable
# >>> lst4 = list(range(3))
# >>> lst4
# [0, 1, 2]
# >>> lst3
# ['a', 'b', 'c']
# >>> lst[2]
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# IndexError: list index out of range
# >>> lst3[2]
# 'c'


##########可变数据类型  vs  不可变数据类型###############
# >>> a = "abc"
# >>> a[1]
# 'b'
# >>> a[1]="f"
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: 'str' object does not support item assignment
# >>> a = "afc"
# >>> lst = ["abc", 1, 5,"edf"]
# >>> lst[0]
# 'abc'
# >>> lst[0]="xxx"
# >>> lst
# ['xxx', 1, 5, 'edf']
# >>> lst = ["abc", 1, 5,"edf"]
# >>> id(lst)
# 140707706430664
# >>> lst[0]="xxx"
# >>> id(lst)
# 140707706430664

#########索引 切片##############
# >>> lst = ["a","c",1,8,"h","g",4.5,None]
# >>> lst[-8:8]
# ['a', 'c', 1, 8, 'h', 'g', 4.5, None]
# >>> lst[:-3]
# ['a', 'c', 1, 8, 'h']
# >>> lst[2:8:-2]
# []

#slice对象
# >>> lst = ["abc", "haha", 3, "g", 1.0, "nn"]
# >>> lst2 = ["1","3","a","b","x","y"]
# >>> s1 = slice(0,3,1)
# >>> lst[s1]
# ['abc', 'haha', 3]
# >>> lst2[s1]
# ['1', '3', 'a']


#######################列表的基本操作#########################
#增加-append   每次在列表末尾增加一个元素
# >>> lst
# ['abc', 'haha', 3, 'g', 1.0, 'nn', 'xixi']
# >>> id(lst)
# 140686523012040
# >>> lst.append("mm")
# >>> lst
# ['abc', 'haha', 3, 'g', 1.0, 'nn', 'xixi', 'mm']
# >>> lst.append([4,5,6])
# >>> lst
# ['1', '3', '4', [4, 5, 6]]

#增加-insert  在指定下标位置添加元素
# >>> lst.insert(2,"yy")
# >>> lst
# ['1', '3', 'yy', '4', [4, 5, 6]]
# >>> lst.insert(10,"xx")
# >>> lst
# ['1', '3', 'yy', '4', [4, 5, 6], 'xx']

#增加-extend
#参数需要传入可迭代对象（可以别for循环获取的）
# >>> lst.extend("abc")
# >>> lst
# ['1', '3', 'yy', '4', [4, 5, 6], 'xx', 'a', 'b', 'c']
# >>> lst.extend(["1","2","3"])
# >>> lst
# ['1', '3', 'yy', '4', [4, 5, 6], 'xx', 'a', 'b', 'c', '1', '2', '3']

# +号拼接  两个列表拼接生成一个新列表
# >>> lst3 = lst+lst2
# >>> lst3
# ['a', 'b', 'c', 1, 2, 3]

#删除 -pop  指定下标删除，默认删除最后一个，并且将删除的元素作为返回值返回
# >>> lst
# ['a', 'b', 'c']
# >>> lst.pop()
# 'c'
# >>> lst
# ['a', 'b']
# >>> remove_item = lst.pop()
# >>> remove_item
# 'b'
# >>> lst
# ['a']
# >>> lst2
# [1, 2, 3]
# >>> lst2.pop(1)
# 2
# >>> lst2
# [1, 3]



#有一条船，30个人。船超载，只能载15个人。
#30个人进行编号，从1开始，每数到第8个就下船，最后输出哪些编号的人下船了


# Member = list(range(1,31))
# print(Member)
# Out = 7
# for i in range(15):
#     if Out+1 <= len(Member):
#         print(Member.pop(Out))
#         Out += 7
#     else:
#         Out = Out -(len(Member)-1 )-1
#         print(Member.pop(Out))
#         Out += 7

# member = list(range(1,31))
# while len(member) > 15:
#     i = 1
#     while i<8:
#         member.append(member.pop(0))
#         i+=1
#     print(member.pop(0))


#删除 - remove  指定元素删除 -- 删除列表中匹配的第一个元素， 如果列表中没有指定元素，会报错
# >>> lst
# ['aa', 'bb', 'aa', 'cc']
# >>> lst.remove("aa")
# >>> lst
# ['bb', 'aa', 'cc']
# >>> lst.remove("dd")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# ValueError: list.remove(x): x not in list

#删除 - del  关键字删除 --删除引用
#只要切片能切出来，那del都可以删除
# >>> lst
# ['bb', 'aa', 'cc']
# >>> del lst[0]
# >>> lst
# ['aa', 'cc']
# >>> lst
# ['aa', 'cc']
# >>> lst = ["a","b","c","d"]
# >>> del lst[:2]
# >>> lst
# ['c', 'd']

# 删除  -clear  清空列表
# >>> lst
# ['c', 'd']
# >>> lst.clear()
# >>> lst
# []


#修改
#可以通过下标和切片来进行增加、修改
#当切出来的列表为空的时候，就在start位置开始插入相应的list值
#当切出来的列表不为空的时候，则替换查找出来的lst元素

#接受一个可迭代对象
# >>> lst[2]=5
# >>> lst
# [1, 2, 5, 'a', 'b', 'c']
# >>> lst[1:1]
# []
# >>> lst[1:1]='xyz'
# >>> lst
# [1, 'x', 'y', 'z', 2, 5, 'a', 'b', 'c']
# >>> lst[1:6]
# ['x', 'y', 'z', 2, 5]
# >>> lst[1:6]='mn'
# >>> lst
# [1, 'm', 'n', 'a', 'b', 'c']

# 反转 -- reverse  或者负切片
# >>> lst
# ['a', 'b', 'c', 1, 2, 3]
# >>> lst.reverse()
# >>> lst
# [3, 2, 1, 'c', 'b', 'a']
# >>> lst[::-1]
# ['a', 'b', 'c', 1, 2, 3]
# >>> lst
# [3, 2, 1, 'c', 'b', 'a']

#排序  -- sort  默认升序
#列表中的数据类型一致
# >>> lst
# [3, 2, 1, 'c', 'b', 'a']
# >>> lst.sort()
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# TypeError: '<' not supported between instances of 'str' and 'int'
# >>> lst = [3,6,7,2,4,1,2]
# >>> lst.sort()
# >>> lst
# [1, 2, 2, 3, 4, 6, 7]
# >>> lst.sort(reverse=True)  #反向排序
# >>> lst
# [7, 6, 4, 3, 2, 2, 1]


#统计 -- len、count、 index

# >>> len(lst)
# 5
# >>> lst = ["a","a","f","e","g"]
# >>> lst.count("a")
# 2
# >>> lst.index("a")
# 0
# >>> lst.index("f")
# 2
# >>>
# >>> lst.index("z")
# Traceback (most recent call last):
#   File "<stdin>", line 1, in <module>
# ValueError: 'z' is not in list

#########遍历##############
lst = ["aa","bb",11,33]
for i in lst:
    print(i)

for i in enumerate(lst):
    print(i)

for i,j in enumerate(lst):
    print(f"{i} --- > {j}")

######成员关系判断
# in
# not in
print("aa" in lst)
print("cc" in lst)

####
#列表 [2,3,4,5,6,7,8]
#从键盘接受用户的输入，输入一个整数 10
#返回哪两个数相加等于10 返回下标   0,6

